package it.unibo.deis.lia.mmhc.tcf;
//
import it.unibo.deis.lia.mmhc.spm.Service;

import java.util.Vector;

public class Connection {

	private String remoteIP;
	private String localIp;
	private int searchServiceRequestNumber;
	private int findedServiceNumber;
	private int numberHop;
	private Vector<Hop> hopService;
	
	private Vector<String> alreadySeenHop;
	private Vector<Service> alreadySeenService;

	public Connection(String remoteIP, String localIp) {
		super();
		this.remoteIP = remoteIP;
		this.localIp = localIp;
		this.searchServiceRequestNumber = 0;
		this.findedServiceNumber = 0;
		this.numberHop = 0;
		this.hopService = new Vector<Hop>();
		this.alreadySeenHop = new Vector<String>();
		this.alreadySeenService = new Vector<Service>();
	}

	public void addHop(String name) {
		hopService.add(new Hop(name));
	}

	public void searchServiceRequestNumberIncrease() {
		searchServiceRequestNumber++;
	}

	public void findedServiceNumberIncrease(Service service) {
		boolean alreadyseen = false;
		for (int i = 0; i < alreadySeenService.size(); i++) {
			if (alreadySeenService.elementAt(i).getFornitore()
					.equalsIgnoreCase(service.getFornitore())
					&& alreadySeenService.elementAt(i).getName()
							.equalsIgnoreCase(service.getName())
					&& alreadySeenService.elementAt(i).getPort() == service
							.getPort()) {
				alreadyseen = true;
				break;

			}
		}
		if (!alreadyseen) {
			findedServiceNumber++;
			alreadySeenService.add(service);
		}

	}

	public void numberHopIncrease(String name) {
		boolean alreadyseen = false;
		for (int i = 0; i < alreadySeenHop.size(); i++) {
			if (alreadySeenHop.elementAt(i).equalsIgnoreCase(name)) {
				alreadyseen = true;
				break;
			}
		}
		if (!alreadyseen) {
			alreadySeenHop.add(name);
			numberHop++;
		}
	}
	
	public Vector<Hop> getHopService() {
		return hopService;
	}


	public void increaseServiceOfferd(String name) {
		Hop hop = getHop(name);
		if (hop == null) {
			hop = new Hop(name);
			hopService.add(hop);
		}
		hop.numberServiceOfferdIncrease();
	}

	public void increaseServiceUsed(String name) {
		Hop hop = getHop(name);
		if (hop == null) {
			hop = new Hop(name);
			hopService.add(hop);
		}
		hop.numberServiceUsedIncrease();
	}

	public double getUtilityIndex() {
		// /qui metterò come si calcola l'indice di utilità di questa
		// paritcolare connessione!!!
		return 0.1;
	}

	private Hop getHop(String name) {
		for (int i = 0; i < hopService.size(); i++) {
			if (hopService.elementAt(i).getName().equalsIgnoreCase(name)) {
				return hopService.elementAt(i);
			}

		}
		return null;
	}

	public String getRemoteIP() {
		return remoteIP;
	}

	public String getLocalIp() {
		return localIp;
	}

	public int getSearchServiceRequestNumber() {
		return searchServiceRequestNumber;
	}

	public int getFindedServiceNumber() {
		return findedServiceNumber;
	}

	public int getNumberHop() {
		return numberHop;
	}

	public class Hop {

		private String name;
		private int numberServiceOfferd;
		private int numberServiceUsed;

		public Hop(String name) {
			super();
			this.name = name;
			this.numberServiceOfferd = 0;
			this.numberServiceUsed = 0;
		}

		public void numberServiceOfferdIncrease() {
			numberServiceOfferd++;
		}

		public void numberServiceUsedIncrease() {
			numberServiceUsed++;
		}

		public String getName() {
			return name;
		}

		public int getNumberServiceOfferd() {
			return numberServiceOfferd;
		}

		public int getNumberServiceUsed() {
			return numberServiceUsed;
		}
	}

}
